<?php
include_once 'common.inc';

if ($_POST['method'] == 'new') {
    $gid = addNewGame($uid);
    $game['gid'] = $gid;
    $game['scheme'] = 0;
    $game['caption'] = "Расчет $gid";
    $game['comments'] = "";
    $game['public'] = 0;
    setGameOptions($gid, $game);
    $values = fillDefaultValues($defaultCounts);
    setValues($gid, $values);
} else if ($_POST['method'] == 'choose') {
    if ($gid !== $_POST['gid'])
        if (verifyGid($_POST['gid']) && isOwner($uid, $_POST['gid'])) {
            $gid = $_POST['gid'];
            setCurrentGid($uid, $gid);
        }
} else if ($_POST['method'] == 'change') {
    $ok = 1;
    if ($gid !== $_POST['gid']) {
        if (verifyGid($_POST['gid']) && isOwner($uid, $_POST['gid'])) {
            $gid = $_POST['gid'];
            setCurrentGid($uid, $gid);
        } else $ok = 0;
    }
    if ($ok && is_numeric($_POST['scheme']) && $_POST['scheme'] >= 0 && $_POST['scheme'] < 3)
        setGameOptions ($gid, array("scheme" => $_POST['scheme'], "caption" => $_POST['caption'], "comments" => $_POST['comments'],
            "public" => $_POST['public']));
} else if ($_POST['method'] == 'delete') {
    if (verifyGid($_POST['gid']) && isOwner($uid, $_POST['gid']))
        deleteGame($_POST['gid']);
} else if ($_POST['method'] == 'add') {
    if (verifyGid($_POST['gid'])) {
        if (loadIfPublic($_POST['gid'], $ruid, $game, $values)) {
            $gid = addNewGame($uid);
            $game['gid'] = $gid;
            $game['public'] = 0;
            setGameOptions($gid, $game);
            setValues($gid, $values);
        }
    }
}

?>
<script type="text/javascript">
    window.location = "<?php echo $sitePrefix;?>/";
</script>
